import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;


public class Table {
        ArrayList<Player_comp> players;
        Deck deck;
    Scanner sc = new Scanner(System.in);

        public Table(int Playersnum, int Botsnum) {
                players = new ArrayList<>();
                deck = new Deck();
                while(Playersnum-- > 0)
                {
                        addPlayer(new Player("Gracz "+(Playersnum+1)));
                }
        while(Botsnum-- > 0)
        {
            addPlayer(new Bot("Bot "+(Botsnum+1)));
        }
        }

        void addPlayer(Player_comp pl)
        {
                if(players.size() < 4)
                        players.add(pl);
        }
       
        void dealCards()
        {
                for(Player_comp pl : players)
                {
                        pl.hand = deck.typeHand();
                }
        }
       
        void getPlayerCards(ArrayList<Card> CardsToChange, Player_comp pl)
        {
                pl.hand.delCards(CardsToChange);
                int numOfCardsToChange = CardsToChange.size();
                while (numOfCardsToChange-- > 0)
                {
                        pl.hand.addCard(deck.typeCard());
                }
                deck.addCards(CardsToChange);
        }
       
        Player_comp settleWinner()
        {
                Collections.sort(players);
                return players.get(players.size()-1);
        }
       
        void showTable()
        {
                for(Player_comp pl : players)
                {
                        System.out.print(pl + ": ");
                        System.out.print(pl.hand);
            System.out.println("  = " + pl.hand.pickSet());
        }
        }

    public void gameplay()
    {
        dealCards();

        for(Player_comp actualPlayer : players)
        {
            if(actualPlayer instanceof Bot)
            {
                System.out.println("Ruch wykonuje Bot...");
                getPlayerCards(actualPlayer.pickCardsToChange(), actualPlayer);
                System.out.println("Bot wymienil " + actualPlayer.getnumOfExCards() + " kart(y).");
                System.out.println("Nacisnij {ENTER}, zeby zakonczyc jego ruch: ");
                sc.nextLine();
            }
            else
            {
                System.out.println("");
                System.out.println("Teraz jest kolej: " + actualPlayer);
                System.out.print("Prosze innych graczy o zamkniecie oczu. Nacinij Enter jesli jestes gotowy.");
                sc.nextLine();
                simulateScreenClearing();
                System.out.println("Twoje Karty: " + actualPlayer.pickCards());
                System.out.println("-----------------------------------------------");
                CardChange(actualPlayer);
                sc.nextLine();
                System.out.print("Nacisnij {ENTER}, zeby zakonczyc kolejke: ");
                sc.nextLine();
                simulateScreenClearing();
                System.out.println(actualPlayer + " wymienil: " + actualPlayer.getnumOfExCards() + " kart(y)." );

            }

        }

        Player_comp winner = settleWinner();
        showTable();
        System.out.println("---------------------------------------------------");
        System.out.println("Zwyciezyl: " + winner + " " + " uklad: " + winner.whatSet());
        System.out.print("Nacisnij {ENTER}, zeby zakonczyc Gre: ");
        sc.nextLine();
        simulateScreenClearing();

    }
    private void simulateScreenClearing()
    {
        int i = 50;
        while(i-- > 0)
            System.out.println("");
    }
    public void CardChange(Player_comp actualPlayer)
    {
        System.out.print("Czy chcesz wymieniac karty?['T'|'N']: ");
        String odp = sc.next();
        if(odp.equals("T"))
        {
        	getPlayerCards(actualPlayer.pickCardsToChange(), actualPlayer);
            System.out.println("Twoje karty po wymianie: " + actualPlayer.pickCards());
        }
        else{
            System.out.println("Nie wymienasz kart.");
        }

    }
}

